package com.huidao.service;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.List;
import java.util.Scanner;

import com.huidao.data.dataBase;
import com.huidao.domain.User;

/**
 * 用户操作逻辑的最高父类
 * 
 * @author Administrator
 *
 */
public class UserService {

	// 将集合中的已有的密码和用户名数据传入
	protected static List<String> registUserList = dataBase.getRegistList();
	protected static List<String> list = dataBase.getList();
	// 将集合中的已注册用户数据传入
	protected static List<User> userList = dataBase.getUserList();
	protected static List<User> managerList = dataBase.getManagerList();
	// 将注册用户信息写入已注册信息中
	protected static BufferedWriter bwUser = dataBase.getBwUser();
	protected static BufferedWriter bw =dataBase.getBw();
	// 将注册用户信息读出
	protected static BufferedReader brUser = dataBase.getBrUser();
	protected static BufferedReader br =dataBase.getBr();
	// 键盘录入信息
	protected static Scanner sc = new Scanner(System.in);
	static{
		readMes();
	}

	/**
	 * 将用户信息读取存储在集合中
	 */
	public static void readMes() {
		String line = null;
		try {
			while((line=br.readLine())!=null){
				list.add(line);
			}
			while ((line = brUser.readLine()) != null) {
				registUserList.add(line);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// 判断输入的用户名是否存在
	public static boolean exist(String userName,List<String> list) {
		// TODO Auto-generated method stub
		if (userName == null) {
			System.out.println("输入错误");
			userName = sc.next();
			exist(userName,list);
			return false;
		}
		for (String string : list) {
			String[] registName = string.split("=");
			if (userName.equals(registName[0])) {
				System.out.println("用户名已存在，请重新输入");
				return true;
			}
		}
		return false;
	}

	/**删除用户*/
	public boolean del(){
		System.out.println("请输入要删除的用户名");
		String userName = sc.next();
		if (userName==null) {
			System.out.println("输入错误，请重新输入");
			del();
			return false;
		}
		if (exist(userName, registUserList)) {
			registUserList.remove(userName);
			return true;
		}
		System.out.println("不存在，若要继续操作请输入1");
		if (sc.nextInt()==1) {
			del();
		}
		return false;
	}
	/**修改用户密码*/
	public  boolean change(){
		System.out.println("请输入要修改的用户名");
		String userName = sc.next();
		if (userName==null) {
			System.out.println("输入错误，请重新输入");
			del();
			return false;
		}
		for (String string : registUserList) {
			String []str = string.split("=");
			if (userName.equals(str[0])) {
				registUserList.remove(string);
				System.out.println("请输入修改后的密码：");
				str[1]= sc.next();
				registUserList.add(str[0]+"="+str[1]);
				return true;
			}
		}
		return false;
	}
	/**
	 * @return the regisUsertList
	 */
	public static List<String> getRegistList() {
		return registUserList;
	}

	/**
	 * @return the userList
	 */
	public static List<User> getUserList() {
		return userList;
	}		
}
